package com.company.test17;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Created by Administrator on 2016.9.19.
 */
public class Test1701 extends JFrame {
    private ColorPanel colorPanel;
    private MessagePanel messagePanel;
    private ButtonPanel buttonPanel;

    public Test1701(){
        colorPanel=new ColorPanel();
        messagePanel=new MessagePanel();
        buttonPanel=new ButtonPanel();
        this.add(colorPanel,BorderLayout.NORTH);
        this.add(messagePanel,BorderLayout.CENTER);
        this.add(buttonPanel,BorderLayout.SOUTH);
    }

    public static void main(String[] args){
        JFrame frame=new Test1701();
        frame.setTitle("选择表");
        frame.setLocationRelativeTo(null);
        frame.setSize(400,400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    class ColorPanel extends JPanel{

        JRadioButton redJrb=new JRadioButton("red",false);
        JRadioButton yelloJrb=new JRadioButton("red",false);
        JRadioButton whiteJrb=new JRadioButton("red",false);
        JRadioButton grayJrb=new JRadioButton("red",false);
        JRadioButton greenJrb=new JRadioButton("red",false);

        public ColorPanel(){
            ButtonGroup group=new ButtonGroup();
            group.add(redJrb);
            group.add(yelloJrb);
            group.add(whiteJrb);
            group.add(grayJrb);
            group.add(greenJrb);

            redJrb.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Test1701.this.messagePanel.setColor(Color.red);
                }
            });
            yelloJrb.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Test1701.this.messagePanel.setColor(Color.yellow);
                }
            });
            whiteJrb.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Test1701.this.messagePanel.setColor(Color.white);
                }
            });
            grayJrb.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Test1701.this.messagePanel.setColor(Color.gray);
                }
            });
            greenJrb.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Test1701.this.messagePanel.setColor(Color.green);
                }
            });
            this.add(redJrb);
            this.add(yelloJrb);
            this.add(whiteJrb);
            this.add(grayJrb);
            this.add(greenJrb);
        }
    }

    static class MessagePanel extends JPanel{
        int x;
        int y;
        private String message;
        private Color color;

        public MessagePanel(){
            x=10;
            y=200;
            message="Welcome to Java";
            color=Color.WHITE;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            g.drawString(message,x,y);
            this.setBackground(color);
        }

        public void setColor(Color color){
            this.color=color;
            repaint();
        }
    }

    class ButtonPanel extends JPanel{
        private JButton left;
        private JButton right;

        public ButtonPanel(){
            left=new JButton("<=");
            right=new JButton("=>");

            left.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    super.mousePressed(e);
                    Test1701.this.messagePanel.x=Test1701.this.messagePanel.x-5;
                    Test1701.this.messagePanel.repaint();
                }
            });
            right.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    super.mousePressed(e);
                    Test1701.this.messagePanel.x=Test1701.this.messagePanel.x+5;
                    Test1701.this.messagePanel.repaint();
                }
            });

            this.add(left);
            this.add(right);
        }
    }
}
